﻿using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.IO;
using System.Windows.Forms;
using Sunisoft.IrisSkin;
using Application = System.Windows.Forms.Application;

namespace Clock
{
    public partial class ClockForm : Form
    {
        private readonly PrivateFontCollection fontCollection = new PrivateFontCollection();
        private readonly SkinEngine skinEngine = new SkinEngine();
        private IntPtr clockPtr;
        private bool showDot;
        readonly DateForm dateForm = new DateForm();
        readonly NoteForm noteForm = new NoteForm();

        public ClockForm()
        {
            InitializeComponent();

            ReplaceSystemClock();

            skinEngine.SkinFile = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "MacOS.ssk");

            var p = Process.GetCurrentProcess();
            p.MaxWorkingSet = (IntPtr)750000;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            NativeMethods.RegisterHotKey(Handle, Keys.X.GetHashCode(), NativeMethods.MOD_WIN, (uint)Keys.X);

            BackColor = Color.FromArgb(14, 140, 233);
            ForeColor = Color.White;

            //fontCollection.AddFontFile("digifaw.ttf");
            if (fontCollection.Families.Length > 0)
            {
                //Font = new Font(fontCollection.Families[0], 7);
            }
        }

        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams createParams = base.CreateParams;
                createParams.Style |= 0x40000000;
                createParams.Style |= 0x04000000;
                createParams.Style &= ~0x00010000;
                createParams.Style &= ~0x02000000;
                createParams.ExStyle &= ~0x00010000;
                return createParams;
            }
        }

        private bool ReplaceSystemClock()
        {
            Location = new Point(0, 0);

            clockPtr = NativeMethods.FindWindow("Shell_TrayWnd", null);
            if (clockPtr == IntPtr.Zero) return false;
            clockPtr = NativeMethods.FindWindowEx(clockPtr, IntPtr.Zero, "TrayNotifyWnd", null);
            if (clockPtr == IntPtr.Zero) return false;
            clockPtr = NativeMethods.FindWindowEx(clockPtr, IntPtr.Zero, "TrayClockWClass", null);
            if (clockPtr == IntPtr.Zero) return false;
            IntPtr result = NativeMethods.SetParent(Handle, clockPtr);
            if (result == IntPtr.Zero)
                return false;

            int style = NativeMethods.GetWindowLong(clockPtr, -16);
            style |= 0x02000000;
            NativeMethods.SetWindowLong(clockPtr, -16, style);
            return true;
        }

        protected override void OnMouseDoubleClick(MouseEventArgs e)
        {
            base.OnMouseDoubleClick(e);
            ShowDateForm(false);
        }

        private void ShowDateForm(bool isMouseHover)
        {
            int x = Screen.PrimaryScreen.WorkingArea.Right - dateForm.Size.Width;
            int y = Screen.PrimaryScreen.WorkingArea.Bottom - dateForm.Size.Height;
            dateForm.IsMouseHover = isMouseHover;
            //dateForm.Location = new Point(x, y);
            dateForm.ShowDialog(this);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            string time = showDot ? DateTime.Now.ToString("HH:mm") : DateTime.Now.ToString("HH mm");
            showDot = !showDot;
            string date = DateTime.Now.ToString("yy/MM/dd");
            SizeF timeSize = e.Graphics.MeasureString(time, Font);
            SizeF dateSize = e.Graphics.MeasureString(date, Font);

            float startY = (ClientSize.Height - timeSize.Height - dateSize.Height) / 2;

            e.Graphics.DrawString(time, Font, Brushes.White, (e.ClipRectangle.Width - timeSize.Width) / 2, startY);

            e.Graphics.DrawString(date, Font, Brushes.White, (e.ClipRectangle.Width - dateSize.Width) / 2,
                                  startY + timeSize.Height);
        }

        private void ClockTimerTick(object sender, EventArgs e)
        {
            if (!NativeMethods.IsWindow(clockPtr))
            {
                if (!ReplaceSystemClock()) return;
            }
            if (DateTime.Now.Second == 0)
            {
                DateToolTip.SetToolTip(this, DateTime.Now.ToString());
            }
            Refresh();
        }

        protected void OnPaintBackgroundB(PaintEventArgs e)
        {
            e.Graphics.Clear(Color.White);

            var cb = new ColorBlend();
            //要渐变的颜色
            var colors = new[]
                             {
                                 Color.FromArgb(200, 201, 246, 255),
                                 Color.FromArgb(200, 179, 230, 255),
                                 Color.FromArgb(200, 166, 222, 255),
                                 Color.FromArgb(200, 234, 255, 255)
                             };
            //颜色分布位置，即一个颜色，处理渐变过程中的哪个位置，最大为1
            var positions = new[] { 0, 0.45F, 0.5F, 1.0F };
            cb.Colors = colors;
            cb.Positions = positions;

            var lb = new LinearGradientBrush(ClientRectangle, BackColor, Color.Transparent,
                                             LinearGradientMode.BackwardDiagonal);
            //lb.InterpolationColors = cb;
            e.Graphics.FillRectangle(lb, ClientRectangle);
        }

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);

            if (m.Msg == NativeMethods.WM_HOTKEY)
            {
                if(!noteForm.Visible)
                noteForm.Show();
                else
                {
                    noteForm.Activate();
                    noteForm.BringToFront();
                }
            }
        }

    }
}